package com.starblink.post.detail.ui

import android.os.Bundle
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.starblink.android.basic.base.activity.BaseTVMActivity
import com.starblink.android.basic.ext.bindFragments
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.toBean
import com.starblink.android.basic.ext.toJson
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.util.post.PostListDataHolder
import com.starblink.basic.route.RoutePage
import com.starblink.post.databinding.ActivityPostDetailBinding
import com.starblink.rocketreserver.fragment.PostD
import com.starblink.rocketreserver.fragment.PostF


@Route(path = RoutePage.Post.PAGE_PIC)
class PostDetailTVMActivity : BaseTVMActivity<ActivityPostDetailBinding, DetailVM>() {

    override fun initViewBinding(): ActivityPostDetailBinding =
        ActivityPostDetailBinding.inflate(layoutInflater)

    //    @Autowired(name = RoutePage.Post.POST_LIST)
    @JvmField
    var listPostMain: List<PostF>? = null

    @Autowired(name = RoutePage.Post.POST_ID)
    @JvmField
    var postId: String? = null

    @Autowired(name = RoutePage.Post.TRACE_ID)
    @JvmField
    var traceId: String? = null

    @Autowired(name = RoutePage.Post.POST)
    @JvmField
    var post: PostF? = null

    @Autowired(name = "index")
    @JvmField
    var originalIndex: Int = 0

    @Autowired(name = RoutePage.Search.ARG_KEY_WORD)
    @JvmField
    var searchWord: String? = null

    @Autowired(name = RoutePage.Post.ARG_SCENE)
    @JvmField
    var scene: Int = 0//前置进入场景

    @Autowired(name = RoutePage.Post.POST_PAGE)
    @JvmField
    var searchPageNo: Int = 0//其它场景加载页数

    @Autowired(name = RoutePage.Post.TOPIC_ID)
    @JvmField
    var topicId: String = ""//话题跳转id

    @Autowired(name = RoutePage.Post.POST_SEARCH_TYPE)
    @JvmField
    var searchType: Int = -1//其它场景加载类型

    var currentIndex = 0

    var isSingle = true//是否是单一图文

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        viewModel.originalPostId = postId.toString()
        viewModel.fragmentList.value = arrayListOf()

        if (scene == RoutePage.Post.SCENE.Discover.value || scene == RoutePage.Post.SCENE.SEARCH.value || scene == RoutePage.Post.SCENE.TOPIC.value) {
            listPostMain = PostListDataHolder.getInstance().getList()
        }
        if (listPostMain != null) {//处理传入集合 过滤其他类型
            isSingle = false

            //过滤图文数据
            var list = listPostMain!!.filter {
                it.contentType == "1"
            } as ArrayList<PostF>
            currentIndex = list.indexOfFirst { it.id == postId }

            if (list.size > 0) {
                viewModel.initData(list, traceId = traceId ?: "")
            }
        } else {
//                postId = "297285497562906624"//带话题 预发
//                postId = "262052646680535040"//带hashtag 预发
//                postId = "294182879163510784"//挂载商品 test
//                postId = "259453179955961856"//多评论 test
            if (post != null) {
                viewModel.fragmentList.value = arrayListOf(
                    PostDetailFragment.newInstance(
                        postId!!,
                        traceId,
                        post!!.toJson().toBean<PostD>()
                    )
                )
            } else {
                if (!postId.isNullOrEmpty()) {
                    viewModel.fragmentList.value = arrayListOf(
                        PostDetailFragment.newInstance(postId!!, traceId, null)
                    )
                }
            }

            viewBinding.vp.bindFragments(
                supportFragmentManager,
                viewModel.fragmentList.value!!, count = 0
            )
        }

        if (searchPageNo != 0) {
            viewModel.scenePageNo = searchPageNo
            viewModel.sceneId = topicId
        }
        viewModel.searchType = searchType


        viewBinding.vBackIcon.click {
            finish()
        }

        if (!isSingle) {
            viewBinding.vp.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {
                }

                override fun onPageScrollStateChanged(state: Int) {}

                override fun onPageSelected(position: Int) {
                    currentIndex = position
                    if (position == (viewModel.fragmentList.value?.size ?: 0) - 1 && !isSingle) {
                        viewModel.getRecommend(scene, searchWord)
                    }
                }
            })

            viewModel.fragmentList.observe(this) {
                if (viewModel.fragmentList.value!!.isNotEmpty()) {
                    if (viewBinding.vp.adapter != null) {
                        viewBinding.vp.adapter!!.notifyDataSetChanged()
                    } else {
                        viewBinding.vp.bindFragments(
                            supportFragmentManager,
                            viewModel.fragmentList.value!!,
                            count = if (scene == RoutePage.Post.SCENE.Discover.value) 2 else 1
                        )
                    }
                    if (currentIndex > 0) {
                        viewBinding.vp.setCurrentItem(currentIndex, false)
                    }
                }
            }
        }
    }

    override fun isStatusBarLightMode(): Boolean {
        return false
    }

    override fun onResume() {
        super.onResume()
        pageExposure(
            SpmPageDef.DetailRoute,
            params = mapOf("itemId" to (postId ?: ""), "contentType" to "1")
        )
    }

    override fun onPause() {
        pageDisappear(
            SpmPageDef.DetailRoute,
            params = mapOf("itemId" to (postId ?: ""), "contentType" to "1")
        )
        super.onPause()
    }
}